package com.llsydn.data.outfile;

import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.llsydn.hello.R;

import java.io.*;

/**
 * @author lilinshen
 * @title SD卡外部file存储
 * @description 请填写相关描述
 * @date 2018/9/25 11:48
 */
public class OutFileActivity extends AppCompatActivity {
    private EditText et_of_name;
    private EditText et_of_content;

    /**
     * 应用运行用到的数据文件(如图片)可以保存到sd卡中
     * 文件类型: 任意
     * 数据保存的路径:
     * 路径1: /storage/sdcard/Android/data/packageName/files/
     * 路径2: /storage/sdcard/xxx/
     * 路径1 :其它应用可以访问,应用卸载时删除
     * 路径2 : 其它应用可以访问, 应用卸载时不会删除
     * 必须保证sd卡挂载在手机上才能读写, 否则不能操作
     */

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_out_file);

        et_of_name = findViewById(R.id.et_of_name);
        et_of_content = findViewById(R.id.et_of_content);
    }

    /**
     * 保存
     * /storage/sdcard/Android/data/packageName/files/
     *
     * @param view
     */
    public void save(View view) throws Exception {
        //1.判断sd卡状态，如果是挂载的状态才继续，否则提示
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            //2.读取输入的文件名/内容
            String filname = et_of_name.getText().toString();
            String content = et_of_content.getText().toString();
            //3.得到指定文件的outputStream
            //1).得到sd卡下的files路径
            String filePath = getExternalFilesDir(null).getAbsolutePath();
            //2).组成完整路径
            String file = filePath + "/" + filname;
            //3).创建FileOutPutStream
            FileOutputStream fos = new FileOutputStream(file);
            //4.写数据
            fos.write(content.getBytes("utf-8"));
            fos.close();
            //5.提示
            Toast.makeText(this, "保存完成", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "SD卡没有挂载", Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 读取
     * /storage/sdcard/Android/data/packageName/files/
     *
     * @param view
     */
    public void read(View view) throws Exception {

        //1.判断sd卡状态，如果是挂载的状态才继续，否则提示
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            //2.读取输入的文件名/内容
            String filname = et_of_name.getText().toString();
            //3.得到指定文件的outputStream
            //1).得到sd卡下的files路径
            String filePath = getExternalFilesDir(null).getAbsolutePath();
            //2).组成完整路径
            String file = filePath + "/" + filname;
            //3).创建FileInputStream
            FileInputStream fis = new FileInputStream(file);
            //4.读取数据
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int len = -1;
            while ((len = fis.read(buffer)) != -1) {
                baos.write(buffer, 0, len);
            }
            String content = baos.toString();
            //5.显示数据
            et_of_content.setText(content);
        } else {
            Toast.makeText(this, "SD卡没有挂载", Toast.LENGTH_SHORT).show();
        }

    }

    //  /storage/sdcard/llsydn/xxx.txt
    public void save2(View v) throws IOException {
        //1. 判断sd卡状态, 如果是挂载的状态才继续, 否则提示
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            //2. 读取输入的文件名/内容
            String fileName = et_of_name.getText().toString();
            String content = et_of_content.getText().toString();
            //3. 得到指定文件的OutputStream
            //1). /storage/sdcard/
            String sdPath = Environment.getExternalStorageDirectory().getAbsolutePath();
            //2). /storage/sdcard/llsydn/(创建文件夹)
            File file = new File(sdPath + "/llsydn");
            if (!file.exists()) {
                file.mkdirs();//创建文件夹
            }
            //3). /storage/sdcard/llsydn/xxx.txt
            String filePath = sdPath + "/llsydn/" + fileName;
            //4). 创建输出流
            FileOutputStream fos = new FileOutputStream(filePath);
            //4. 写数据
            fos.write(content.getBytes("utf-8"));
            fos.close();
            //5. 提示
            Toast.makeText(this, "保存完成", 0).show();
        } else {
            Toast.makeText(this, "sd卡没有挂载", 0).show();
        }
    }

    public void read2(View v) throws Exception {
        // 1. 判断sd卡状态, 如果是挂载的状态才继续, 否则提示
        if (Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED)) {
            // 2. 读取输入的文件名
            String fileName = et_of_name.getText().toString();
            // 3. 得到指定文件的InputStream
            String sdPath = Environment.getExternalStorageDirectory().getAbsolutePath();
            String filePath = sdPath + "/llsydn/" + fileName;
            FileInputStream fis = new FileInputStream(filePath);
            // 4. 读取数据, 成String
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int len = -1;
            while ((len = fis.read(buffer)) != -1) {
                baos.write(buffer, 0, len);
            }
            String content = baos.toString();
            fis.close();
            // 5. 显示
            et_of_content.setText(content);
        } else {
            Toast.makeText(this, "sd卡没有挂载", 0).show();
        }
    }
}
